using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace FinanceAnalyzer
{
    public class InWork : IMoneyFlow
    {
        private Dictionary<DateTime, MonthlyDB> _monthlyDB;  
        private DateTimeHandler _currentMonthHandler;
        private int _currentMonthIndex;

        static readonly InWork instance = new InWork();

        private InWork()
        {
            _monthlyDB = new Dictionary<DateTime, MonthlyDB>();
            
            _currentMonthHandler = DateTimeHandler.UniqueInstance;
            _currentMonthHandler.MonthChanged += new DateTimeHandler.MonthChangedHandler(UpdateDB);
        }

        public static InWork UniqueInstance
        {
            get { return instance;}
        }

        public void LittleHelper()
        {
            _monthlyDB.Add(_currentMonthHandler.getCurrentDate(), new MonthlyDB());
            _monthlyDB[_currentMonthHandler.getCurrentDate()].Add("test", 3.8);

            DateTime dt = new DateTime(2007, 11, 1);
            _monthlyDB.Add(dt, new MonthlyDB());
            _monthlyDB[dt].Add("sehr sehr grosse ausgabe", 10000);
            _monthlyDB[dt].Add("sehr grosse ausgabe", 1000);
            _monthlyDB[dt].Add("grosse ausgabe", 100);
            _monthlyDB[dt].Add("ausgabe", 10);
            _monthlyDB[dt].Add("kleine ausgabe", 1);
            

            DateTime dt2 = new DateTime(2008, 1, 1);
            _monthlyDB.Add(dt2, new MonthlyDB());
            _monthlyDB[dt2].Add("strom", 57);
            _monthlyDB[dt2].Add("gas", 7);
            _monthlyDB[dt2].Add("heizung", 5);

            _currentMonthIndex = _monthlyDB.Count;
        }


        #region IMoneyFlowReason Member

        public List<DateTime> getYears()
        {
            List<DateTime> yearList = new List<DateTime>();

            foreach (DateTime date in _monthlyDB.Keys)
            {                
                if (!yearList.Contains(date))
                {
                    yearList.Add(date);
                }
            }

            return yearList;
        }
        
        public void Add(string comment, double value)
        {
            _monthlyDB[_currentMonthHandler.getCurrentDate()].Add(comment, value);
        }

        public double Sum()
        {
            return _monthlyDB[_currentMonthHandler.getCurrentDate()].Sum();
        }
        
        public double Sum(String year)
        {
            double temp = 0;

            foreach (DateTime date in getYears())
            {
                if (date.Year.ToString().Equals(year))
                {
                    temp += _monthlyDB[date].Sum();
                }
            }
            return temp;
        }

        public void ExcelExporter()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void Remove()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void UpdateDB(object sender, MonthChangedEventArgs e)
        {
            _monthlyDB.Add(e.Date, new MonthlyDB());
        }

        #endregion
    }
}
